package net.uman.web.util;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.uman.framework.modules.web.struts2.Struts2Utils;

@SuppressWarnings("unchecked")
public class JSONUtils<T extends Object> {
	public  List<T> JSONArrayToBeans(String key,Class cls){
		List<T> results=new ArrayList<T>();
		HttpServletRequest request=Struts2Utils.getRequest();
		String objects=request.getParameter(key);
		JSONArray arr=JSONArray.fromObject(objects);		
		for(int i=0;i<arr.size();i++){
			T obj =(T)JSONObject.toBean(arr.getJSONObject(i), cls);
			results.add(obj);
		}		
		return results;
	}
	// Added by Alex Ma
	//直接从JSONArray字符串生成相对应的VO数组
	public  List<T> JSONArrayStrToBeans(String jsonArrayStr, Class clz){
		List<T> results=new ArrayList<T>();
		JSONArray arr=JSONArray.fromObject(jsonArrayStr);		
		for(int i=0;i<arr.size();i++){
			T obj =(T)JSONObject.toBean(arr.getJSONObject(i), clz);
			results.add(obj);
		}		
		return results;
	}
}
